/******************************************************************
 * 个人博客：https://blog.csdn.net/2302_80277720
 * 嵌入式Linux学习交流群：1005210698
 * 欢迎各位大佬和萌新来加入交流学习
 * Change Logs:
 * Date           Author       Notes
 * 2025-08-24     喝呜昂黄    first version
 * 2025-08-30     喝呜昂黄    修改页翻转，添加日志功能
 ******************************************************************/

#include "stdio.h"
#include <stdint.h>

#include "common.h"
#include "convert.h"

static int get_rga_fmt(image_format_t fmt) {
    switch (fmt) {
        case IMAGE_FORMAT_RGB888:
            return RK_FORMAT_RGB_888;
        case IMAGE_FORMAT_BGR888:
            return RK_FORMAT_BGR_888;
        case IMAGE_FORMAT_ARGB8888:
            return RK_FORMAT_ARGB_8888;
        case IMAGE_FORMAT_ABGR8888:
            return RK_FORMAT_ABGR_8888;
        case IMAGE_FORMAT_XRGB8888:
            return RK_FORMAT_XRGB_8888;
        case IMAGE_FORMAT_XBGR8888:
            return RK_FORMAT_XBGR_8888;
        case IMAGE_FORMAT_YUV420SP_NV12:
            return RK_FORMAT_YCbCr_420_SP;
        case IMAGE_FORMAT_YUV420SP_NV21:
            return RK_FORMAT_YCrCb_420_SP;
        default:
            return -1;
    }
}

// 精简后的RGA图像处理
// 完整位于 /source/utils/image_utils.c ——> covert_image_rga()
int covert(image_buffer_t *src_image, image_buffer_t *dst_image, int rotate) {
    if (src_image->virt_addr == NULL) {
        printf("ERR(%s):src_image->virt_addr  is NULL\n", __func__);
        return -1;
    }

    if (dst_image->virt_addr == NULL) {
        printf("ERR(%s):dst_image->virt_addr is NULL\n", __func__);
        return -1;
    }

    int ret = 0;
    // 源图像信息
    int srcWidth  = src_image->width;
    int srcHeight = src_image->height;
    void *src     = src_image->virt_addr;
    int src_fd    = 0;
    int srcFmt    = get_rga_fmt(src_image->format);

    // 目标图像信息
    int dstWidth  = dst_image->width;
    int dstHeight = dst_image->height;
    void *dst     = dst_image->virt_addr;
    int dst_fd    = 0;
    int dstFmt    = get_rga_fmt(dst_image->format);

    // 图像旋转
    int usage = rotate;
    // int usage = 0;
    IM_STATUS ret_rga = IM_STATUS_NOERROR;

    // 源图像矩形
    im_rect srect = {
        0,
        0,
        srcWidth,
        srcHeight,
    };
    // 目标图像矩形
    im_rect drect = {
        0,
        0,
        dstWidth,
        dstHeight,
    };

    // 源RGA缓冲区
    rga_buffer_t rga_buf_src;
    // 目标RGA缓冲区
    rga_buffer_t rga_buf_dst;

    // 处理源缓冲区
    if (src_fd > 0)
        rga_buf_src = wrapbuffer_fd(src_fd, srcWidth, srcHeight, srcFmt, srcWidth, srcHeight);
    else
        rga_buf_src = wrapbuffer_virtualaddr(src, srcWidth, srcHeight, srcFmt, srcWidth, srcHeight);

    // 处理目标缓冲区
    if (dst_fd > 0)
        rga_buf_dst = wrapbuffer_fd(dst_fd, dstWidth, dstHeight, dstFmt, dstWidth, dstHeight);
    else
        rga_buf_dst = wrapbuffer_virtualaddr(dst, dstWidth, dstHeight, dstFmt, dstWidth, dstHeight);

    // RGA处理
    ret_rga = improcess(rga_buf_src, rga_buf_dst, (rga_buffer_t){0}, srect, drect, (im_rect){0}, usage);
    if (ret_rga <= 0) {
        printf("Error on improcess STATUS=%d\n", ret_rga);
        printf("RGA error message: %s\n", imStrError((IM_STATUS)ret_rga));
        ret = -1;
    }

    return ret;
}